<template>
  <div class="blue-wrap">
    <div class="user-box">
      <span>姓名：{{name}}</span>
      <span>性别：{{sex}}</span>
      <span>身份证号码：{{certNo}}</span>
      <span>联系电话：{{mobile}}</span>
      <span>入职日期：{{beginTime}}</span>
    </div>

    <div class="blue-box">
      <span class="title">家住地址:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入详细地址" class="blue" v-model="userAddress" maxlength="15"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">其他联系电话:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入其他联系电话" class="blue" v-model="userPhone" maxlength="11"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">工号:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入工号" class="blue" v-model="workNum" maxlength="15"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">入职部门:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入入职部门" class="blue" v-model="workDepartment" maxlength="10"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">组长姓名:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入组长姓名" class="blue" v-model="bossName" maxlength="5"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">组长联系方式:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入组长联系方式" class="blue" v-model="bossPhone" maxlength="11"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">宿舍楼号:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入宿舍楼号" class="blue" v-model="userHostel" maxlength="10"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">员工来源:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入员工来源" class="blue" v-model="userComeFrom" maxlength="10"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">供应商电话:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入供应商电话" class="blue" v-model="comeFormTel" maxlength="11"/>
      </div>
    </div>
    <div class="blue-box">
      <span class="title">员工工价:</span>
      <div class="blue-con">
        <input type="text" placeholder="请输入员工工价" class="blue" v-model="workMoney" maxlength="6"/>
      </div>
    </div>

    <div class="btn-box">
      <button type="button" class="btn" @click="confirm">确定</button>
    </div>
  </div>
</template>

<script>
  import {baseUrl} from "../../../config/env"

  export default {
    data() {
      return {
        id: "",
        userId: "",
        mechanismId: "",
        name: "",
        sex: "男",
        certNo: "",
        mobile: "",
        beginTime: this.$route.query.beginTime,
        userSign: "",
        workNum: "",//工号
        userAddress: "",//家庭住址
        userPhone: "",//用户其他联系电话
        workDepartment: "",//所在部门
        bossName: "",//组长姓名
        bossPhone: "",//组长联系电话
        userHostel: "",//用户宿舍
        userComeFrom: "",//用户来源
        comeFormTel: "",//来源人电话
        workMoney: "",//工价
      }
    },
    mounted() {
      this.id = this.$route.query.id;
      this.userId = this.$route.query.userId;
      this.mechanismId = this.$route.query.mechanismId;
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("success"));

      this.name = this.$route.query.name;
      this.certNo = this.$route.query.number;
      if (this.certNo != '' && this.certNo != null) {
        let num = "";
        if (this.certNo.length == 15) {
          num = this.certNo.substring(14, 15);
        } else {
          num = this.certNo.substring(17, 18);
        }
        if (num % 2 == 0) {
          this.sex = '女';
        }
      }
      this.mobile = this.$route.query.tel;
      this.queryEmployBlue();
    },
    methods: {
      queryEmployBlue() {
        this.$vux.loading.show({
          text: '加载中',
        });
        let time = (new Date()).getTime();
        let url = baseUrl + "/resident/query_user_registration?timestamp=" + time;
        let param = {"sgin": this.userSign, "timestamp": time};
        let signature = this.$signature(url, param);


        this.axios.get(baseUrl + "/resident/query_user_registration?sgin=" + this.userSign + "&experienceId=" + this.id + "&type=1" + "&auth=" + signature + "&timestamp=" + time).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0 && resultObj.data != null) {
            this.workNum = resultObj.data.workNum;//工号
            this.userAddress = resultObj.data.userAddress;//家庭住址
            this.userPhone = resultObj.data.userPhone;//用户其他联系电话
            this.workDepartment = resultObj.data.workDepartment;//所在部门
            this.bossName = resultObj.data.bossName;//组长姓名
            this.bossPhone = resultObj.data.bossPhone;//组长联系电话
            this.userHostel = resultObj.data.userHostel;//用户宿舍
            this.userComeFrom = resultObj.data.userComeFrom;//用户来源
            this.comeFormTel = resultObj.data.comeFormTel;//来源人电话
            this.workMoney = resultObj.data.workMoney;//工价
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      check() {//校验信息
        let reg1 = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (this.userPhone != '' && this.userPhone != null) {
          if (!reg1.test(this.userPhone)) {
            this.$vux.toast.text("请输入正确的其他联系电话", "middle");
            return;
          }
        }
        if (this.bossPhone != '' && this.bossPhone != null) {
          if (!reg1.test(this.bossPhone)) {
            this.$vux.toast.text("请输入正确的组长联系方式", "middle");
            return;
          }
        }
        if (this.comeFormTel != '' && this.comeFormTel != null) {
          if (!reg1.test(this.comeFormTel)) {
            this.$vux.toast.text("请输入正确的供应商电话", "middle");
            return;
          }
        }
        if (this.bossName != '' && this.bossName != null) {
          let reg2 = /^[\u2E80-\u9FFF]+$/;
          if (!reg2.test(this.bossName)) {
            this.$vux.toast.text("请输入正确的组长姓名", "middle");
            return;
          }
        }
        if (this.workMoney != '' && this.workMoney != null) {
          let reg3 = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
          if (!reg3.test(this.workMoney)) {
            this.$vux.toast.text("请输入正确的员工工价", "middle");
            return;
          }
        }
        return true;
      },
      confirm() {//确定
        if (this.check()) {
          this.$vux.loading.show({
            text: '加载中',
          });
          let data = {
            "experienceId": this.id,
            "userId": this.userId,
            "workNum": this.workNum,
            "userAddress": this.userAddress,
            "userPhone": this.userPhone,
            "workDepartment": this.workDepartment,
            "bossName": this.bossName,
            "bossPhone": this.bossPhone,
            "userHostel": this.userHostel,
            "workEndTime": this.workEndTime,
            "workEndDetails": this.workEndDetails,
            "userComeFrom": this.userComeFrom,
            "comeFormTel": this.comeFormTel,
            "workMoney": this.workMoney
          };

          let time = (new Date()).getTime();
          let url = baseUrl + "/resident/insert_user_work_record?timestamp=" + time;
          let param = {"sgin": this.userSign, "timestamp": time};
          let signature = this.$signature(url, param);

          this.axios.post(baseUrl + "/resident/insert_user_work_record?sgin=" + this.userSign + "&type=1" + "&auth=" + signature + "&timestamp=" + time, data).then((res) => {
            let resultObj = res.data;
            this.$vux.loading.hide();
            if (resultObj.code == 0) {
              if (resultObj.data == 0) {
                this.$vux.toast.text("员工信息登记失败，请重试", "middle");
              } else {
                this.$router.push({path: "/enterpriselist", query: {"mechanismId": this.mechanismId, "type": 1}});
              }
            } else {
              this.$vux.toast.text(resultObj.msg, "middle");
            }
          }).catch(() => {
            this.$vux.loading.hide();
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"

  .blue-wrap
    wh(100%, auto)
    background: rgba(242, 242, 242, 1)
    .user-box
      display: flex
      flex-direction: column
      margin-bottom: 0.267rem
      wh(100%, 4.64rem)
      background: rgba(255, 255, 255, 1)
      span
        margin-left: 0.32rem
        margin-bottom: 0.413rem
        size-color(0.427rem, rgba(153, 153, 153, 1))
        &:first-child
          margin-top: 0.467rem
          font-weight: 500
          color: rgba(51, 51, 51, 1)
        &:last-child
          margin-bottom: 0
    .blue-box
      display: flex
      align-items: center
      wh(100%, 1.28rem)
      border-bottom: 0.027rem solid rgba(242, 242, 242, 1)
      background: rgba(255, 255, 255, 1)
      &:nth-child(3)
        margin-bottom: 0.267rem
      &:nth-child(8)
        margin-bottom: 0.267rem
      .title
        margin-left: 0.32rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
      .blue-con
        margin-right: 0.32rem
        display: flex
        align-items: center
        flex: 1
        height: 1.28rem
        size-color(0.427rem, rgba(51, 51, 51, 1))
        .blue
          flex: 1
          height: 0.6rem
          line-height: 0.6rem
          text-align: right
    .btn-box
      display: flex
      justify-content: center
      align-items: center
      width: 100%
      height: 1.28rem
      margin-top: 0.96rem
      padding-bottom: 0.64rem
      .btn
        width: 90.4%
        height: 1.28rem
        line-height: 1.28rem
        background: rgba(60, 175, 255, 1)
        font-size: 0.453rem
        color: rgba(255, 255, 255, 1)
        font-weight: 500
        border-radius: 0.16rem
</style>
